<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 15:11
 */
namespace app\admin\controller;
use app\admin\model\AdminUser;
use app\admin\model\Bank;
use app\admin\model\GetOccupation;
use app\admin\model\Tables;
use app\admin\model\UpFile;
use think\Db;
use think\Request;

class Del
{
    /**删除课程分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function classes(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入要删除的分类']);
        };
        Db::name('branch')->where('tables_class','in',$id)->delete();
        Db::name('three_level')->where('classes_id','in',$id)->delete();
//        Db::name('bank')->where('classes_id','in',$id)->delete();
        Bank::del('classes_id','in',$id);
        if(Db::name('classes')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除课程分支
     * @param Request $request
     * @return \think\response\Json
     */
    public function branch(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入要删除的分类']);
        }
        Db::name('three_level')->where('branchid','in',$id)->delete();
//        Db::name('bank')->where('branch_id','in',$id)->delete();
        Bank::del('branch_id','in',$id);
        if(Db::name('branch')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>200,'msg'=>'删除失败']);
        }
    }

    /**删除题目
     * @param Request $request
     * @return \think\response\Json
     */
    public function question_bank(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('bank')->where('id','in',$id)->select();
        if(Db::name('bank')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                if($value['picture']){
                    UpFile::delFile($value['picture']);
                }
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除职业
     * @param Request $request
     * @return \think\response\Json
     */
    public function occupation(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入要删除的id']);
        }
        $id =  GetOccupation::gets($id);
//        return $id;
        if(Db::name('occupation')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除等级
     * @param Request $request
     * @return \think\response\Json
     */
    public function grade(Request $request){
        $id = $request->param('id',null);
        if(Db::name('grade')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除vip
     * @param Request $request
     * @return \think\response\Json
     */
    public function vip_price(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入删除的id']);
        }
        $data = Db::name('vip_price')->where('id','in',$id)->select();
        if(Db::name('vip_price')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                UpFile::delFile($value['vip_picture']);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除第三类分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function three_level(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入要删除的id']);
        }
        Db::name('bank')->where('three_level_id','in',$id)->delete();
        if(Db::name('three_level')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除模式
     * @param Request $request
     * @return \think\response\Json
     */
    public function situation(Request $request){
        $id = $request->param('id',null);
        Bank::del('type','in',$id);
        if(Db::name('situation')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除设置项
     * @param Request $request
     * @return \think\response\Json
     */
    public function system(Request $request){
        $id = $request->param('id',null);
        if(Db::name('system')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除后台权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function admin_power(Request $request){
        $id = $request->param('id',null);
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        if(Db::name('admin_power')->where('id','in',$id)->delete()){
            Db::name('admin_user')->where('admin_ power','in',$id)->delete();
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function admin_user(Request $request){
        $id = $request->param('id',null);
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        $data = Db::name('admin_user')->where('id','in',$id)->select();
        if(Db::name('admin_user')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                if($value['admin_portrait']){
                    UpFile::delFile($value['admin_portrait']);
                }
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function user(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('user')->where('id','in',$id)->select();
        if(Db::name('user')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                if($value['this_portrait']){
                    UpFile::delFile($value['this_portrait']);
                }
            }
            Db::name('news')->where('user_id','in',$id)->delete();
            Db::name('change')->where('user_id','in',$id)->delete();
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除消息
     * @param Request $request
     * @return \think\response\Json
     */
    public function news(Request $request){
        $id = $request->param('id',null);
        if(Db::name('news')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**删除订单
     * @param Request $request
     * @return \think\response\Json
     */
    public function forward(Request $request){
        $id = $request->param('id',null);
        if(Db::name('forward')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }
}